/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JObjectPanel.java
 *
 * Created on 2010-apr-28, 14:16:31
 */

package foalvleditor.gui;

import foalvleditor.core.ImageSwitcher;
import foalvleditor.core.impl.LevelModel;
import foalvleditor.core.impl.SelectionModel;
import foalvleditor.gameobjects.AbstractGameObject;
import java.awt.Color;
import java.awt.Image;
import java.awt.Point;
import javax.swing.ImageIcon;
import org.jdesktop.application.Action;

/**
 *
 * @author Mikael
 */
public class JObjectPanel extends javax.swing.JPanel {
    /**
	 *
	 */
	private static final long serialVersionUID = 1L;
	private AbstractGameObject goa;
    private boolean hardSelection=false;

    /** Creates new form JObjectPanel */
    public JObjectPanel() {
        super();
        initComponents();
    }

    public JObjectPanel (AbstractGameObject goa) throws CloneNotSupportedException{
        this();
        setObject(goa);
    }

     /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */

    private void initComponents() {

        iconButton = new javax.swing.JButton();
        deleteButton = new javax.swing.JButton();
        positionXSpinner = new javax.swing.JSpinner();
        positionYSpinner = new javax.swing.JSpinner();
        layerSpinner = new javax.swing.JSpinner();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();

        setMaximumSize(new java.awt.Dimension(260, 60));
        setMinimumSize(new java.awt.Dimension(260, 60));
        setName("Form"); // NOI18N
        setPreferredSize(new java.awt.Dimension(260, 60));
        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                formMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                formMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                formMouseExited(evt);
            }
        });

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(foalvleditor.core.FoaLvlEditorApp.class).getContext().getActionMap(JObjectPanel.class, this);
        iconButton.setAction(actionMap.get("changeImage")); // NOI18N
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(foalvleditor.core.FoaLvlEditorApp.class).getContext().getResourceMap(JObjectPanel.class);
        iconButton.setText(resourceMap.getString("iconButton.text")); // NOI18N
        iconButton.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        iconButton.setBorderPainted(false);
        iconButton.setFocusPainted(false);
        iconButton.setMaximumSize(new java.awt.Dimension(35, 35));
        iconButton.setMinimumSize(new java.awt.Dimension(35, 35));
        iconButton.setName("iconButton"); // NOI18N
        iconButton.setPreferredSize(new java.awt.Dimension(35, 35));

        deleteButton.setAction(actionMap.get("deleteObject")); // NOI18N
        deleteButton.setIcon(resourceMap.getIcon("deleteButton.icon")); // NOI18N
        deleteButton.setText(resourceMap.getString("deleteButton.text")); // NOI18N
        deleteButton.setBorderPainted(false);
        deleteButton.setContentAreaFilled(false);
        deleteButton.setFocusPainted(false);
        deleteButton.setMaximumSize(new java.awt.Dimension(35, 35));
        deleteButton.setMinimumSize(new java.awt.Dimension(35, 35));
        deleteButton.setName("deleteButton"); // NOI18N
        deleteButton.setPreferredSize(new java.awt.Dimension(35, 35));
        deleteButton.setPressedIcon(resourceMap.getIcon("deleteButton.pressedIcon")); // NOI18N
        deleteButton.setRolloverIcon(resourceMap.getIcon("deleteButton.rolloverIcon")); // NOI18N

        positionXSpinner.setModel(new javax.swing.SpinnerNumberModel(0, 0, 455, 1));
        positionXSpinner.setEditor(new javax.swing.JSpinner.NumberEditor(positionXSpinner, ""));
        positionXSpinner.setFocusCycleRoot(true);
        positionXSpinner.setName("positionXSpinner"); // NOI18N
        positionXSpinner.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                positionXSpinnerStateChanged(evt);
            }
        });
        positionXSpinner.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                positionXSpinnerPropertyChange(evt);
            }
        });

        positionYSpinner.setModel(new javax.swing.SpinnerNumberModel(0, 0, 320, 1));
        positionYSpinner.setName("positionYSpinner"); // NOI18N
        positionYSpinner.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                positionYSpinnerStateChanged(evt);
            }
        });

        layerSpinner.setModel(new javax.swing.SpinnerNumberModel(0, 0, 3, 1));
        layerSpinner.setName("layerSpinner"); // NOI18N
        layerSpinner.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                layerSpinnerStateChanged(evt);
            }
        });

        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setFont(resourceMap.getFont("jLabel2.font")); // NOI18N
        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setFont(resourceMap.getFont("jLabel3.font")); // NOI18N
        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(iconButton, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(positionXSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(positionYSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(layerSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(deleteButton, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(31, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(positionXSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(positionYSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(layerSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(iconButton, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(deleteButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(13, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    public final void setObject(AbstractGameObject goa){
        this.goa=goa;
        Image img = goa.getBitmap().getBufferedImage().getScaledInstance(25, 25, java.awt.Image.SCALE_SMOOTH);
        this.iconButton.setIcon(new ImageIcon(img));
        positionXSpinner.setValue(Integer.valueOf(goa.getPosition().x));
        positionYSpinner.setValue(Integer.valueOf(goa.getPosition().y));
        layerSpinner.setValue(Integer.valueOf(goa.getLayer()));
        this.repaint();
    }

    private void positionXSpinnerStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_positionXSpinnerStateChanged
         if(positionXSpinner.getValue() instanceof Integer)
         {
            Integer iii=(Integer) positionXSpinner.getValue();
            if (iii!=goa.getPosition().x){
                goa.setPosition(new Point(iii, goa.getPosition().y));
                levelModel.getView().repaintAll();
            }
         }
         else positionXSpinner.setValue((Integer)goa.getPosition().x);
    }//GEN-LAST:event_positionXSpinnerStateChanged

    private void positionXSpinnerPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_positionXSpinnerPropertyChange
    }//GEN-LAST:event_positionXSpinnerPropertyChange

    private void positionYSpinnerStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_positionYSpinnerStateChanged
         if(positionYSpinner.getValue() instanceof Integer)
         {
            Integer iii=(Integer) positionYSpinner.getValue();
            if (iii!=goa.getPosition().y){
                goa.setPosition(new Point(goa.getPosition().x, iii));
              //  instance.changePosition(goa);}
                levelModel.getView().repaintAll();
            }
         }
    }//GEN-LAST:event_positionYSpinnerStateChanged

    private void layerSpinnerStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_layerSpinnerStateChanged
        if(layerSpinner.getValue() instanceof Integer)
         {
            Integer iii=(Integer) layerSpinner.getValue();
            if (iii!=goa.getLayer()){
                goa.setLayer(iii);
                levelModel.getView().repaintAll();
                }
         }
    }//GEN-LAST:event_layerSpinnerStateChanged


    public void hardSelectObject(){
        {   this.setBackground(Color.GRAY);
            this.hardSelection=true;
            this.repaint();
            if (!selectionModel.isSelected(goa)) selectionModel.select(goa);
            levelModel.getView().repaintAll();}
    }

    public void selectObject(){
        this.setBackground(Color.LIGHT_GRAY);
        this.repaint();
        if (!selectionModel.isSelected(goa)) selectionModel.select(goa);
        levelModel.getView().repaintAll();
    }

    public void deselectObject(){
        this.setBackground(new Color(0xE0DFE3));
        this.hardSelection=false;
        this.repaint();
        selectionModel.deselect(goa);
        levelModel.getView().repaintAll();
    }


    private void formMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseClicked
    if(hardSelection)
        deselectObject();
    else hardSelectObject();
    }//GEN-LAST:event_formMouseClicked

    private void formMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseEntered
        if (!hardSelection)
            selectObject();
    }//GEN-LAST:event_formMouseEntered

    private void formMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseExited
        if (!hardSelection)
            deselectObject();
    }//GEN-LAST:event_formMouseExited

    public AbstractGameObject getObject(){
        return this.goa;
    }

    @Action
    public void deleteObject() {
        levelModel.removeObject(goa);
    }

    @Action
    public void changeImage() {
        imageSwitcher.initSwitchImageDialog(this.goa);
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton deleteButton;
    private javax.swing.JButton iconButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JSpinner layerSpinner;
    private javax.swing.JSpinner positionXSpinner;
    private javax.swing.JSpinner positionYSpinner;
    // End of variables declaration//GEN-END:variables
    private static LevelModel levelModel=LevelModel.getInstance();
    private static ImageSwitcher imageSwitcher=ImageSwitcher.getInstance();
    private static SelectionModel selectionModel=SelectionModel.getInstance();
}
